--    Program: 		CAPAREXT_RECEIPT_APPLICATION.sql
--    Description:  Extract AR Receipt Application information 
--    Revision:     1st     
--    Parameter: 	1. set of books short name e.g. SOB_C999

set doc off;      
set verify off;
set serveroutput on size 1000000;
whenever sqlerror exit failure rollback;
alter session set nls_date_format='YYYYMMDD HH24:MI:SS';

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
                    		
   l_utl_file       		utl_file.file_type;
   l_filename       		varchar2(255);
   l_pathname       		varchar2(100);
                    		
   l_ngo_sob				varchar2(20);
   
   l_set_of_books_id		number;
                    		  
   cursor cur_receipt_application (p_set_of_books_id number) is
      select * from ar_receivable_applications_all
      where  set_of_books_id = p_set_of_books_id;

begin
   l_pathname := '/tmp';
   l_filename := 'CAPAR_RECEIPT_APPLICATION.dat';
   l_utl_file := utl_file.fopen(l_pathname, l_filename, 'w');
   
   l_processed_count := 0;
   l_rejected_count  := 0;
   
   l_ngo_sob := '&1';
   
   begin
     select set_of_books_id
     into   l_set_of_books_id
     from   gl_sets_of_books
     where  upper(short_name) = upper(l_ngo_sob);
   exception
     when others then
       dbms_output.put_line(l_ngo_sob||': set of books name not found');
       --raise;
   end;      

   for cur in cur_receipt_application(l_set_of_books_id) loop   	
     
     -- output data 
     utl_file.put_line( l_utl_file, 
       cur.RECEIVABLE_APPLICATION_ID       || chr(9) ||                                                             
       cur.LAST_UPDATED_BY                 || chr(9) ||                                                             
       cur.LAST_UPDATE_DATE                || chr(9) ||                                                             
       cur.CREATED_BY                      || chr(9) ||                                                             
       cur.CREATION_DATE                   || chr(9) ||                                                             
       cur.AMOUNT_APPLIED                  || chr(9) ||                                                             
       cur.AMOUNT_APPLIED_FROM             || chr(9) ||                                                             
       cur.TRANS_TO_RECEIPT_RATE           || chr(9) ||                                                             
       cur.GL_DATE                         || chr(9) ||                                                             
       cur.CODE_COMBINATION_ID             || chr(9) ||                                                             
       cur.SET_OF_BOOKS_ID                 || chr(9) ||                                                             
       cur.DISPLAY                         || chr(9) ||                                                             
       cur.APPLY_DATE                      || chr(9) ||                                                             
       cur.APPLICATION_TYPE                || chr(9) ||                                                             
       cur.STATUS                          || chr(9) ||                                                             
       cur.PAYMENT_SCHEDULE_ID             || chr(9) ||                                                             
       cur.LAST_UPDATE_LOGIN               || chr(9) ||                                                             
       cur.CASH_RECEIPT_ID                 || chr(9) ||                                                             
       cur.APPLIED_CUSTOMER_TRX_ID         || chr(9) ||                                                             
       cur.APPLIED_CUSTOMER_TRX_LINE_ID    || chr(9) ||                                                             
       cur.APPLIED_PAYMENT_SCHEDULE_ID     || chr(9) ||                                                             
       cur.CUSTOMER_TRX_ID                 || chr(9) ||                                                             
       cur.LINE_APPLIED                    || chr(9) ||                                                             
       cur.TAX_APPLIED                     || chr(9) ||                                                             
       cur.FREIGHT_APPLIED                 || chr(9) ||                                                             
       cur.RECEIVABLES_CHARGES_APPLIED     || chr(9) ||                                                             
       cur.EARNED_DISCOUNT_TAKEN           || chr(9) ||                                                             
       cur.UNEARNED_DISCOUNT_TAKEN         || chr(9) ||                                                             
       cur.DAYS_LATE                       || chr(9) ||                                                             
       cur.APPLICATION_RULE                || chr(9) ||                                                             
       cur.GL_POSTED_DATE                  || chr(9) ||                                                             
       cur.COMMENTS                        || chr(9) ||                                                             
       cur.ATTRIBUTE_CATEGORY              || chr(9) ||                                                             
       cur.ATTRIBUTE1                      || chr(9) ||                                                             
       cur.ATTRIBUTE2                      || chr(9) ||                                                             
       cur.ATTRIBUTE3                      || chr(9) ||                                                             
       cur.ATTRIBUTE4                      || chr(9) ||                                                             
       cur.ATTRIBUTE5                      || chr(9) ||                                                             
       cur.ATTRIBUTE6                      || chr(9) ||                                                             
       cur.ATTRIBUTE7                      || chr(9) ||                                                             
       cur.ATTRIBUTE8                      || chr(9) ||                                                             
       cur.ATTRIBUTE9                      || chr(9) ||                                                             
       cur.ATTRIBUTE10                     || chr(9) ||                                                             
       cur.POSTABLE                        || chr(9) ||                                                             
       cur.POSTING_CONTROL_ID              || chr(9) ||                                                             
       cur.ACCTD_AMOUNT_APPLIED_FROM       || chr(9) ||                                                             
       cur.ACCTD_AMOUNT_APPLIED_TO         || chr(9) ||                                                             
       cur.ACCTD_EARNED_DISCOUNT_TAKEN     || chr(9) ||                                                             
       cur.ATTRIBUTE11                     || chr(9) ||                                                             
       cur.ATTRIBUTE12                     || chr(9) ||                                                             
       cur.ATTRIBUTE13                     || chr(9) ||                                                             
       cur.ATTRIBUTE14                     || chr(9) ||                                                             
       cur.ATTRIBUTE15                     || chr(9) ||                                                             
       cur.CONFIRMED_FLAG                  || chr(9) ||                                                             
       cur.PROGRAM_APPLICATION_ID          || chr(9) ||                                                             
       cur.PROGRAM_ID                      || chr(9) ||                                                             
       cur.PROGRAM_UPDATE_DATE             || chr(9) ||                                                             
       cur.REQUEST_ID                      || chr(9) ||                                                             
       cur.USSGL_TRANSACTION_CODE          || chr(9) ||                                                             
       cur.USSGL_TRANSACTION_CODE_CONTEXT  || chr(9) ||                                                             
       cur.EARNED_DISCOUNT_CCID            || chr(9) ||                                                             
       cur.UNEARNED_DISCOUNT_CCID          || chr(9) ||                                                             
       cur.ACCTD_UNEARNED_DISCOUNT_TAKEN   || chr(9) ||                                                             
       cur.REVERSAL_GL_DATE                || chr(9) ||                                                             
       cur.CASH_RECEIPT_HISTORY_ID         || chr(9) ||                                                             
       cur.ORG_ID                          || chr(9) ||
       cur.TAX_CODE                        || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE1               || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE2               || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE3               || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE4               || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE5               || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE6               || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE7               || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE8               || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE9               || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE10              || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE11              || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE12              || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE13              || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE14              || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE15              || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE16              || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE17              || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE18              || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE19              || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE20              || chr(9) ||                                                             
       cur.GLOBAL_ATTRIBUTE_CATEGORY       || chr(9) ||                                                             
       cur.CONS_INV_ID                     || chr(9) ||                                                             
       cur.CONS_INV_ID_TO                  || chr(9) ||                                                             
       cur.RULE_SET_ID                     || chr(9) ||                                                             
       cur.LINE_EDISCOUNTED                || chr(9) ||                                                             
       cur.TAX_EDISCOUNTED                 || chr(9) ||                                                             
       cur.FREIGHT_EDISCOUNTED             || chr(9) ||                                                             
       cur.CHARGES_EDISCOUNTED             || chr(9) ||                                                             
       cur.LINE_UEDISCOUNTED               || chr(9) ||                                                             
       cur.TAX_UEDISCOUNTED                || chr(9) ||                                                             
       cur.FREIGHT_UEDISCOUNTED            || chr(9) ||                                                             
       cur.CHARGES_UEDISCOUNTED            || chr(9) ||                                                             
       cur.RECEIVABLES_TRX_ID              || chr(9) ||                                                             
       cur.ON_ACCOUNT_CUSTOMER             || chr(9) ||                                                             
       cur.MRC_AMOUNT_APPLIED              || chr(9) ||                                                             
       cur.MRC_AMOUNT_APPLIED_FROM         || chr(9) ||                                                             
       cur.MRC_DISPLAY                     || chr(9) ||                                                             
       cur.MRC_STATUS                      || chr(9) ||                                                             
       cur.MRC_PAYMENT_SCHEDULE_ID         || chr(9) ||                                                             
       cur.MRC_CASH_RECEIPT_ID             || chr(9) ||                                                             
       cur.MRC_GL_POSTED_DATE              || chr(9) ||                                                             
       cur.MRC_POSTING_CONTROL_ID          || chr(9) ||                                                             
       cur.MRC_ACCTD_AMOUNT_APPLIED_FROM   || chr(9) ||                                                             
       cur.MRC_ACCTD_AMOUNT_APPLIED_TO     || chr(9) ||                                                             
       cur.MRC_ACCTD_EARNED_DISC_TAKEN     || chr(9) ||                                                             
       cur.MRC_ACCTD_UNEARNED_DISC_TAKEN   || chr(9) ||                                                             
       cur.EDISC_TAX_ACCT_RULE             || chr(9) ||                                                             
       cur.UNEDISC_TAX_ACCT_RULE           || chr(9) ||                                                             
       cur.LINK_TO_TRX_HIST_ID             || chr(9) ||                                                             
       cur.LINK_TO_CUSTOMER_TRX_ID         || chr(9) ||                                                             
       cur.APPLICATION_REF_TYPE            || chr(9) ||                                                             
       cur.APPLICATION_REF_ID              || chr(9) ||                                                             
       cur.APPLICATION_REF_NUM             || chr(9) ||                                                             
       cur.CHARGEBACK_CUSTOMER_TRX_ID      || chr(9) ||                                                             
       cur.SECONDARY_APPLICATION_REF_ID    || chr(9) ||                                                             
       cur.PAYMENT_SET_ID                  || chr(9) ||                                                             
       cur.APPLICATION_REF_REASON          || chr(9) ||                                                             
       cur.CUSTOMER_REFERENCE 
	 );                        
                               
	 l_processed_count := l_processed_count + 1;
   end loop;

   if l_processed_count = 0 then   
     l_rejected_count := l_rejected_count + 1;
     l_err_msg := l_ngo_sob || ': AR Receipt Application not found';
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPAREXT_RECEIPT_APPLICATION', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;

   utl_file.fclose(l_utl_file);
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/

--exit
